package com.block.datapush.service.apipush.utils;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import com.block.common.enums.BaseTypeInfoEnum;
import org.apache.commons.lang3.StringUtils;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.*;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

import static com.block.common.encrypt.enums.EncryptTypeEnum.RSA;
import static com.block.datapush.service.apipush.utils.WangXunRsaUtils.SIGN_ALGORITHMS;


/**
 * 小财贷款 双授权
 */
public class XiaoCaiDaiKuanUtil {

    //性别
    public static Integer getSex(Integer sex) {
        if (sex == 1) {
            return 1;
        }
        return 0;
    }

    //芝麻分
    public static Integer getZhimaScore(String zhima) {
        if (StrUtil.equals(BaseTypeInfoEnum.Zhima.zm001.getValue(), zhima)) {
            return 0;
        } else if (StrUtil.equals(BaseTypeInfoEnum.Zhima.zm003.getValue(), zhima)) {
            return 2;
        }else if (StrUtil.equals(BaseTypeInfoEnum.Zhima.zm004.getValue(), zhima)) {
            return 3;
        }
        return 5;
    }

    //公积金
    public static Integer getGjj(String gongjijin) {
        if (StrUtil.equals(BaseTypeInfoEnum.Gjj.gjj001.getValue(), gongjijin)) {
            return 1;
        }else if(StrUtil.equals(BaseTypeInfoEnum.Gjj.gjj002.getValue(), gongjijin)){
            return 2;
        }
        return 3;
    }

    //社保
    public static Integer getSocialInsurance(String shebao) {
        if (StrUtil.equals(BaseTypeInfoEnum.Shebao.sb001.getValue(), shebao)) {
            return 1;
        }else if (StrUtil.equals(BaseTypeInfoEnum.Shebao.sb002.getValue(), shebao)) {
            return 2;
        }
        return 3;
    }

    //投保情况
    public static Integer getbaodan(String baodan) {
        if (StrUtil.equals(BaseTypeInfoEnum.Baoxian.bx001.getValue(), baodan)) {
            return 0;
        }
        return 1;
    }

    //房产情况
    public static Integer getHouse(String house) {
        if (StrUtil.equals(BaseTypeInfoEnum.House.hu001.getValue(), house)) {
            return 1;
        }
        return 2;
    }

    //车产情况
    public static Integer getCar(String car) {
        if (StrUtil.equals(BaseTypeInfoEnum.Car.car001.getValue(), car)) {
            return 2;
        }
        return 1;
    }

    //房产情况
    public static Integer getMortgageStatus(String house) {
        if (StrUtil.equals(BaseTypeInfoEnum.House.hu001.getValue(), house) ) {
            return 4;
        }
        return 3;
    }

    //工作情况
    public static Integer getJob(String job) {
        if (StrUtil.equals(BaseTypeInfoEnum.Gongzuo.gz001.getValue(), job)) {
            return 1;
        }else if (StrUtil.equals(BaseTypeInfoEnum.Gongzuo.gz002.getValue(), job)){
            return 4;
        }else if (StrUtil.equals(BaseTypeInfoEnum.Gongzuo.gz003.getValue(), job)){
            return 2;
        }
        return 0;
    }

    public static Integer getloan(String loan) {
        if (StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la001.getValue(), loan)) {
            return 10000;
        } else if (StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la002.getValue(), loan)) {
            return 30000;
        } else if (StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la003.getValue(), loan)) {
            return 50000;
        } else if (StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la004.getValue(), loan)) {
            return 100000;
        }
        return 200000;
    }

    public static String getIp(String ip){
        if (StringUtils.isNotEmpty(ip)){
            return ip;
        }
        return "127.0.0.1";
    }

    //收入
    public static Integer getShouru(String shouru) {
        if (StrUtil.equals(BaseTypeInfoEnum.Shouru.sr001.getValue(), shouru)) {
            return 2;
        }else if (StrUtil.equals(BaseTypeInfoEnum.Shouru.sr002.getValue(), shouru)){
            return 3;
        }else if (StrUtil.equals(BaseTypeInfoEnum.Shouru.sr003.getValue(), shouru)){
            return 5;
        }
        return 6;
    }

    public static Integer getXueli(String xueli) {
        if(StrUtil.equals(BaseTypeInfoEnum.XueLi.xl001.getValue(), xueli)){
            return 0;
        }else if(StrUtil.equals(BaseTypeInfoEnum.XueLi.xl002.getValue(), xueli)){
            return 3;
        }else if(StrUtil.equals(BaseTypeInfoEnum.XueLi.xl003.getValue(), xueli)){
            return 4;
        }else if(StrUtil.equals(BaseTypeInfoEnum.XueLi.xl004.getValue(), xueli)){
            return 5;
        }
        return 6;
    }

    public  static Integer getXinYongKa(String xinYongKa) {
        if (StrUtil.equals(BaseTypeInfoEnum.XinYongKa.xyk001.getValue(), xinYongKa)) {
            return 0;
        }else if(StrUtil.equals(BaseTypeInfoEnum.XinYongKa.xyk002.getValue(), xinYongKa)){
            return 3;
        }
        return 4;
    }


    public static Integer getWeiLi(String weili) {
        if(StrUtil.equals(BaseTypeInfoEnum.WeiLi.wl001.getName(), weili)){
            return 4;
        }
        return 0;
    }

    public static Integer getloanAmount(String loanAmount) {
        if(StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la001.getValue(), loanAmount)) {
            return 3;
        }else if(StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la002.getValue(), loanAmount)) {
            return 5;
        }else if(StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la003.getValue(), loanAmount)) {
            return 10;
        }else if(StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la004.getValue(), loanAmount)) {
            return 20;
        }
        return 25;
    }

    public static Integer getloantime(String loantime) {
        if (StrUtil.equals(BaseTypeInfoEnum.LoanTime.lt001.getValue(), loantime)) {
            return 3;
        } else if (StrUtil.equals(BaseTypeInfoEnum.LoanTime.lt002.getValue(), loantime)) {
            return 6;
        } else if (StrUtil.equals(BaseTypeInfoEnum.LoanTime.lt003.getValue(), loantime)) {
            return 12;
        }
        return 24;

    }

    public static Integer getYongtu(String yongtu) {
        if (StrUtil.equals(BaseTypeInfoEnum.Yongtu.yt007.getValue(), yongtu)) {
            return 0;
        } else if (StrUtil.equals(BaseTypeInfoEnum.Yongtu.yt005.getValue(), yongtu)) {
            return 4;
        } else if (StrUtil.equals(BaseTypeInfoEnum.Yongtu.yt004.getValue(), yongtu)) {
            return 2;
        }else if (StrUtil.equals(BaseTypeInfoEnum.Yongtu.yt003.getValue(), yongtu)) {
            return 3;
        }else if (StrUtil.equals(BaseTypeInfoEnum.Yongtu.yt002.getValue(), yongtu)) {
            return 1;
        }
        return 7;
    }


    public static Integer getbaitiao(String baitiao) {
        if(StrUtil.equals(BaseTypeInfoEnum.Baitiao.bt001.getValue(), baitiao)) {
            return  1;
        }
        return 2;
    }

    public static Integer getyuqi(String yuqi) {
        if (StrUtil.equals(BaseTypeInfoEnum.Yuqi.yq001.getValue(), yuqi)) {
            return 4;
        }else if (StrUtil.equals(BaseTypeInfoEnum.Yuqi.yq002.getValue(), yuqi)) {
            return 3;
        }
        return 2;
    }

    public static Integer gethuabei(String huabei) {
        if(StrUtil.equals(BaseTypeInfoEnum.Huabei.hb001.getValue(), huabei)) {
            return 0;
        }
        return 2;
    }

    /**
     * RSA公钥加密
     * @param encryptData 待加密数据
     * @param publicKey 公钥
     * @return 返回加密数据
     * @throws Exception 异常处理
     */
    public static final String RSA = "RSA";
    public static String encrypt(String encryptData, String publicKey) throws Exception {
        try {
            // Base64Util编码的公钥，需要先decode
            byte[] decoded = Base64.decode(publicKey.getBytes("UTF-8"));
            RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance(RSA).generatePublic(new X509EncodedKeySpec(decoded));
            // RSA加密
            Cipher cipher = Cipher.getInstance(RSA);
            cipher.init(Cipher.ENCRYPT_MODE, pubKey);
            String outStr = Base64.encode(cipher.doFinal(encryptData.getBytes("UTF-8")));
            return outStr;
        } catch (NoSuchAlgorithmException e) {
            throw new NoSuchAlgorithmException("无此加密算法，请检查环境");
        } catch (NoSuchPaddingException e) {
            throw new NoSuchPaddingException("明文数据未找到");
        } catch (InvalidKeyException e) {
            throw new InvalidKeyException("加密秘钥非法，请检查");
        } catch (IllegalBlockSizeException e) {
            throw new IllegalBlockSizeException("明文长度非法");
        } catch (BadPaddingException e) {
            throw new BadPaddingException("明文数据已损坏");
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("其他错误:", e);
        }
    }

    /**
     *
     * @param content 待签名数据
     * @param privateKey 私钥
     * @return 返回签名数据
     * @throws Exception 异常处理
     */
    public static String sign(String content, String privateKey) throws Exception {
        PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64.decode(privateKey.getBytes("UTF-8")));
        KeyFactory keyf = KeyFactory.getInstance(RSA);
        PrivateKey priKey = keyf.generatePrivate(priPKCS8);
        Signature signature = Signature.getInstance(SIGN_ALGORITHMS);
        signature.initSign(priKey);
        signature.update(content.getBytes());
        byte[] signed = signature.sign();
        return java.util.Base64.getEncoder().encodeToString(signed);
    }
}
